using System;
using System.IO;
using PluginFrame;

namespace PcrExperimentProject
{
	public class FormEditProjectOption : FormProjectOption
	{
		private bool mIsAllowToChangeChannelSettings;

		public FormEditProjectOption(IApplication app, IPcrExperimentProject experimentProject, bool isAllowToChangeChannelSettings)
			: base(app, experimentProject.SystemType)
		{
			mExperimentProject = experimentProject;
			Text = mResourceManager.GetString("ProjectOptionString");
			txtRemark.Text = experimentProject.Remark;
			mIsAllowToChangeChannelSettings = isAllowToChangeChannelSettings;
			btnAdvancedOption.Enabled = mIsAllowToChangeChannelSettings;
		}

		protected override void InitializeFormControl()
		{
			base.InitializeFormControl();
			mPcrProjectService.RefreshItemGridContent(mItemGrid, mExperimentProject, !mIsAllowToChangeChannelSettings);
		}

		protected override void InitializeProjectName()
		{
			txtProjectName.MaxLength = mConfigurationService.MaxExperimentNameLength;
			txtProjectName.Text = mExperimentProject.Name;
			txtProjectName.Enabled = true;
			txtSaveAsPath.Text = Path.GetDirectoryName(mExperimentProject.FileName) + "\\";
			txtSaveAsPath.Enabled = true;
			btnSaveAsPath.Enabled = true;
		}

		protected override void InitializeExperimentType()
		{
			base.InitializeExperimentType();
			EnabledExpeimentType(false);
		}

		protected override IPcrExperimentProject CreateProject(PcrSystemType systemType)
		{
			mExperimentProject.SystemType = systemType;
			CreateProjectItems(systemType);
			return mExperimentProject;
		}

		protected override void InitializeProgramControlType()
		{
			if (mExperimentProject.TemperatureControlType == TemperatureControlType.Block)
			{
				rdbBlockControl.Checked = true;
				rdbTubeControl.Checked = false;
			}
			else
			{
				rdbBlockControl.Checked = false;
				rdbTubeControl.Checked = true;
			}
			numVolume.DecimalPlaces = 0;
			numVolume.Value = Convert.ToDecimal(mExperimentProject.ReactionVolume);
			numVolume.Maximum = Convert.ToDecimal(mConfigurationService.MaxReactionVolume);
			numVolume.Minimum = Convert.ToDecimal(mConfigurationService.MinReactionVolume);
			SetReactionVolumeEnabled(!rdbBlockControl.Checked);
			txtUnit.Text = mExperimentProject.Unit;
		}

		protected override bool CheckProjectFileExisted()
		{
			string text = txtSaveAsPath.Text + txtProjectName.Text + ".prj";
			if (!mExperimentProject.FileName.Equals(text))
			{
				return File.Exists(text);
			}
			return false;
		}
	}
}
